package com.gdsz.libbasic.view.ui

import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.view.WindowCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import com.gdsz.libbasic.extensions.getViewBinding
import com.gdsz.libbasic.view.aidl.FragmentToActivityCallBack

abstract class BaseFragment<VB : ViewDataBinding>(
) : Fragment(), BaseBinding<VB> {

    protected   var callBack: FragmentToActivityCallBack?=null
    override fun onAttach(context: Context) {
        super.onAttach(context)
        callBack = if (context is FragmentToActivityCallBack) {
            context
        } else {
            null
        }
    }


    protected var isFirstLoad = true
    protected lateinit var binding: VB
        private set

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = getViewBinding(inflater, container)
        binding.lifecycleOwner = viewLifecycleOwner
        initAdapter()
        onClick()
        listener()
        onViewModel()
        return binding.root
    }




    fun setWindowFullScreen(flag :Boolean,isAppearanceLightStatusBars:Boolean = true){
        requireActivity().window.setDecorFitsSystemWindows(false)
        requireActivity().window.statusBarColor = Color.TRANSPARENT

        if (flag){
            WindowCompat.getInsetsController(requireActivity().window, binding.root).let { insetsController ->
                insetsController.hide(WindowInsetsCompat.Type.navigationBars())
                insetsController.hide(WindowInsetsCompat.Type.statusBars())
                insetsController.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
            }
        }else{
            WindowCompat.getInsetsController(requireActivity().window, binding.root).let { insetsController ->
                insetsController.hide(WindowInsetsCompat.Type.navigationBars())
                insetsController.show(WindowInsetsCompat.Type.statusBars())
                insetsController.systemBarsBehavior = WindowInsetsControllerCompat.BEHAVIOR_SHOW_TRANSIENT_BARS_BY_SWIPE
                insetsController.isAppearanceLightStatusBars = isAppearanceLightStatusBars
                insetsController.isAppearanceLightNavigationBars = false
            }
        }



    }

    open fun onViewModel(){}


    open fun onClick(){


    }

    open fun listener(){


    }
    open fun initAdapter(){


    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.initBinding(savedInstanceState)
    }


    open fun onBackPressed(): Boolean {
        return false
    }

    override fun onDestroy() {
        super.onDestroy()
        if (::binding.isInitialized) {
            binding.unbind()
        }
    }

    override fun onResume() {
        super.onResume()
        if (isFirstLoad) {
            onLoadData()
            // 将数据加载逻辑放到onResume()方法中
            isFirstLoad = false
        }
    }

    open fun onLoadData(){

    }

    //定义一个回调接口
     interface ActivityCallBack {
        fun <T>movementRecordData(data: T)
        fun isLoad(isOk: Boolean)
    }

}